﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MailKit.Net.Smtp;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MimeKit;

namespace TestServiceB.Controllers
{
    ///用配置文件Ocelot.json
    //[Route("api/[controller]/[action]")] 
    //用配置文件test2Ocelot.json
    [Route("apiB/[controller]/[action]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        public IConfiguration Configuration { get; }

        public ValuesController(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1B", "value2B" };
        }

        // GET api/values/5
        [HttpGet] //("{id}")
        public ActionResult<string> Getid(int id)
        {
            return HttpContext.Request.Host.Port + "=>" + $"value={id}";
        }

        // POST api/values
        [HttpPost]
        public string Post(string value)
        {
            return HttpContext.Request.Host.Port + "=>" + Configuration["AppName"] + "=>" + DateTime.Now.ToString();
        }

        [HttpGet]
        public IActionResult Heathle()
        {
            return Ok();
        }

        ///// <summary>
        ///// 发邮件
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost("/notice")]
        //public IActionResult Notice()
        //{
        //    var bytes = new byte[10240];
        //    var i = Request.Body.ReadAsync(bytes, 0, bytes.Length);
        //    var content = System.Text.Encoding.UTF8.GetString(bytes).Trim('\0');
        //    SendEmail(content);
        //    return Ok();
        //}
        ///// <summary>
        ///// 发送邮件
        ///// </summary>
        ///// <param name="content"></param>
        //void SendEmail(string content)
        //{
        //    try
        //    {
        //        dynamic list = Newtonsoft.Json.JsonConvert.DeserializeObject(content);
        //        if (list != null && list.Count > 0)
        //        {
        //            var emailBody = new StringBuilder("健康检查故障:\r\n");
        //            foreach (var noticy in list)
        //            {
        //                emailBody.AppendLine($"--------------------------------------");
        //                emailBody.AppendLine($"Node:{noticy.Node}");
        //                emailBody.AppendLine($"Service ID:{noticy.ServiceID}");
        //                emailBody.AppendLine($"Service Name:{noticy.ServiceName}");
        //                emailBody.AppendLine($"Check ID:{noticy.CheckID}");
        //                emailBody.AppendLine($"Check Name:{noticy.Name}");
        //                emailBody.AppendLine($"Check Status:{noticy.Status}");
        //                emailBody.AppendLine($"Check Output:{noticy.Output}");
        //                emailBody.AppendLine($"--------------------------------------");
        //            }
        //            var message = new MimeMessage();
        //            //这里只是是测试邮箱，请不要发垃圾邮件，谢谢
        //            message.From.Add(new MailboxAddress("163账号名称", "163账号"));
        //            message.To.Add(new MailboxAddress("账号名称", "账号"));

        //            message.Subject = "作业报警";
        //            message.Body = new TextPart("plain") { Text = emailBody.ToString() };
        //            using (var client = new SmtpClient())
        //            {

        //                client.ServerCertificateValidationCallback = (s, c, h, e) => true;
        //                client.Connect("smtp.163.com", 25, false);
        //                client.AuthenticationMechanisms.Remove("XOAUTH2");
        //                client.Authenticate("163账号名称", "授权码");
        //                client.Send(message);
        //                client.Disconnect(true);
        //            }
        //        }
        //        else
        //        {
        //            Send163Email();
        //        }
        //    }
        //    catch
        //    {

        //    }
        //}

        //private void Send163Email()
        //{
        //    var emailBody = new StringBuilder("500W人民币:\r\n");
        //    emailBody.AppendLine($"--------------------------------------");
        //    emailBody.AppendLine($"你激动了吗");
        //    emailBody.AppendLine($"赶紧拿起你的电话");
        //    emailBody.AppendLine($"拨打10086");
        //    emailBody.AppendLine($"大奖就是你的了");
        //    //emailBody.AppendLine($"Node:");
        //    //emailBody.AppendLine($"Service ID:");
        //    //emailBody.AppendLine($"Service Name:");
        //    //emailBody.AppendLine($"Check ID:");
        //    //emailBody.AppendLine($"Check Name:");
        //    //emailBody.AppendLine($"Check Status:");
        //    //emailBody.AppendLine($"Check Output:");
        //    emailBody.AppendLine($"--------------------------------------");
        //    var message = new MimeMessage();
        //    //这里只是是测试邮箱，请不要发垃圾邮件，谢谢
        //    //这里只是是测试邮箱，请不要发垃圾邮件，谢谢
        //    message.From.Add(new MailboxAddress("163账号名称", "163账号"));
        //    message.To.Add(new MailboxAddress("账号名称", "账号"));

        //    message.Subject = "作业报警";
        //    message.Body = new TextPart("plain") { Text = emailBody.ToString() };
        //    using (var client = new SmtpClient())
        //    {

        //        client.ServerCertificateValidationCallback = (s, c, h, e) => true;
        //        client.Connect("smtp.163.com", 25, false);
        //        client.AuthenticationMechanisms.Remove("XOAUTH2");
        //        client.Authenticate("163账号名称", "授权码");
        //        client.Send(message);
        //        client.Disconnect(true);
        //    }
        //}
    }
}
